<template>
  <div class="club-list">
    <div class="item" v-for="(item, index) in list" :key="index">
      <div class="flex">
        <img :src="item.icon"/>
        <div class="inner">
          <div class="title">{{item.clubName}}</div>
          <div class="text">ID：{{item.clubId}}</div>
          <div class="text">人数： {{item.peopleNum}}/{{item.peopleLimit}}</div>
          <div class="text">身份： {{(item.identity === 1 ? '管理员':(item.identity === 2 ? '创建者':'普通会员'))}}</div>
          <div class="text">
            <a href="javascript:;" @click="onClickClub(index)">获取牌局记录</a>
          </div>
        </div>
      </div>
      <div class="body">
        <textarea :value="JSON.stringify(item, null, 2)" cols="30" rows="10"></textarea>
      </div>
    </div>
  </div>
</template>

<script>
  import {getClubList, getRoomHistory, getRoomDetail} from '@/api/demo';
  import {getParamsSign} from '@/utils/util';
  export default {
    name: 'club-list',
    props: {
      deviceId: String,
      sessionToken: String,
      userId: [String, Number]
    },
    data() {
      return {
        form: {
          userId: '',
          sessionToken: '',
          version: '5.7.30.5',
          lang: 'cn',
          deviceType: 1,
          deviceId: '',
          idfa: 0,
          platform: 'h5',
          channel: 0,
          bShowApplying: 1,
        },
        list: [],
        roomHistoryForm: {
          userId: '',
          sessionToken: '',
          clubId: '',
          pageNo: '1',
          playType: '0,1,2',
          personNum: '2,3,4,5,6,7,8,9',
          minGrade: '0',
          maxGrade: '1000',
        },
        roomDetailForm: {
          userId: '',
          sessionToken: '',
          roomId: '',
          playType: '',
        },
      }
    },
    mounted() {
      this.getClubList();
    },
    methods: {
      async onClickClub(index) {
        try {
          const item = this.list[index];
          item.roomHistory = await this.getRoomHistory({clubId: item.clubId});
          if (item.roomHistory.errorCode === 0) {
            item.roomHistory.data.roomViewList.forEach(async (room) => {
              const detail = await this.getRoomDetail({roomId: room.roomId});
              room.roomDetail = detail;
            });
          }
          this.$set(this.list, index, item);
        } catch (error) {
          console.error(error);
        }
      },
      getClubList() {
        let data = {
          ...this.form,
          deviceId: this.deviceId,
          sessionToken: this.sessionToken,
          userId: this.userId,
        };
        delete data.sign;
        data['sign'] = getParamsSign(data)
        getClubList(data).then((res) => {
          this.list = res.data?.data?.clubList || [];

          this.loopGetRoomHistory(this.list);
        })
      },
      loopGetRoomHistory(list) {
        let count = 0;
        list.forEach((item, index) => {
          if ([1, 2].includes(item.identity)) {
            setTimeout(() => {
              count += 1;
              this.onClickClub(index);
            }, count * 100);
          }
        });
      },
      async getRoomHistory(opt) {
        try {
          let data = {
            ...this.roomHistoryForm,
            deviceId: this.deviceId,
            sessionToken: this.sessionToken,
            userId: this.userId,
            ...opt
          };
          data['sign'] = getParamsSign(data)
          const res = await getRoomHistory(data);
          if (res.data.errorCode != 0) {
            alert(res.data.errMsg || '获取房间记录失败');
          }
          return Promise.resolve(res.data);
        } catch (error) {
          console.error(error);
          return Promise.reject(error);
        }
      },
      async getRoomDetail(opt) {
        try {
          let data = {
            ...this.roomDetailForm,
            deviceId: this.deviceId,
            sessionToken: this.sessionToken,
            userId: this.userId,
            ...opt
          };
          data['sign'] = getParamsSign(data)
          const res = await getRoomDetail(data);
          if (res.data.errorCode != 0) {
            alert(res.data.errMsg || '获取房间详情失败');
          }
          return Promise.resolve(res.data);
        } catch (error) {
          console.error(error);
          return Promise.reject(error);
        }
      }
    }
  }
</script>

<style scoped>
.club-list .item {
  padding: 10px;
  margin: 10px 0;
  border-radius: 4px;
  border: 1px solid #999;
}
.club-list .item .flex {
  display: flex;
  flex-direction: row;
  cursor: pointer;
}
.club-list .item img {
  width: 60px;
  height: 60px;
}
.club-list .item .inner {
  margin-left: 10px;
  flex: 1;
  font-size: 14px;
}
.club-list .item .inner .title {
  margin-bottom: 10px;
}
.club-list .item .inner .text {
  margin-right: 10px;
  display: inline-block;
}
.club-list .item .body textarea {
  margin-top: 10px;
  width: 100%;
  box-sizing: border-box;
}
a {
  color: blue;
}
</style>